package com.multiplayer.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.multiplayer.dao.ISeatDao;
import com.multiplayer.dao.IStudyRoomDao;
import com.multiplayer.entity.StudyRoom;
import com.multiplayer.tools.DBTools;
import com.multiplayer.tools.PageData;


/**
 * 
 * @author 温在鑫
 *@version   [版本号, 2020-05-09]
 *自习室操作实现类
 */
public class StudyRoomDaoImpl implements IStudyRoomDao{

	/**
	 * 添加自习室
	 */
	@Override
	public boolean addStudyRoom(StudyRoom studyRoom) {
		// TODO Auto-generated method stub

		String sql="insert into studyroom(rno,ano,rposition,rseatnum,rstatus,rfreeseat,rstime,retime) value(?,?,?,?,?,?,?,?)";
		int n=DBTools.exUpdate(sql,studyRoom.getRno(),studyRoom.getAno(),studyRoom.getRposition(),studyRoom.getRseatnum(),studyRoom.getRstatus(),studyRoom.getRfreeseat(),studyRoom.getRstime(),studyRoom.getRetime());
		//再往seat表中批量添加座位
		ISeatDao seatDao=new SeatDaoImpl();
		seatDao.addSeat(studyRoom.getRseatnum(), studyRoom.getRno());
		return n>0;
	}

	/**
	 * 删除自习室
	 */
	@Override
	public boolean deleteStudyRoomById(String studyRoomId) {
		// TODO Auto-generated method stub
		//删座位
		String sql1="delete from seat where rno=?";
		int n = DBTools.exUpdate(sql1, studyRoomId);
		//删自习室
		boolean flag=false;
		String sql = "delete from studyroom where rno=?";
		int m=DBTools.exUpdate(sql, studyRoomId);
		if(m>0&&n>0)
		{
			flag=true;
		}
		return flag;
	}
	
    /**
     * 修改自习室信息
     */
	@Override
	public boolean editStudyRoom(StudyRoom studyRoom) {
		// TODO Auto-generated method stub
		String sql="update studyroom set ano=?,rposition=?,rseatnum=?,rstatus=?,rfreeseat=?,rstime=?,retime=? where rno=?";
		int n=DBTools.exUpdate(sql, studyRoom.getAno(),studyRoom.getRposition(),studyRoom.getRseatnum(),studyRoom.getRstatus(),studyRoom.getRfreeseat(),studyRoom.getRstime(),studyRoom.getRetime(),studyRoom.getRno());
		return n>0;
	}
	/**
	 * 模糊查询自习室信息
	 */
	@Override
	public List<StudyRoom> queryStudyRoomByKeywords(String keywords) {
		// TODO Auto-generated method stub
		String sql="select * from studyroom where rstatus like ? or ano like ? or rno like ? or rposition like ?";
		List<StudyRoom> list=new ArrayList<StudyRoom>();
		list=(List<StudyRoom>) DBTools.exQuery(sql, StudyRoom.class, keywords,keywords,keywords,"%" + keywords + "%");
		return list;
	}
	
	/**
	 * 通过自习室编号查询自习室信息
	 */
	@Override
	public List<StudyRoom> queryStudyRoomById(String studyRoomId) {
		// TODO Auto-generated method stub
		String sql="select * from studyroom where rno = ?";
		List<StudyRoom> list=new ArrayList<StudyRoom>();
		list=(List<StudyRoom>) DBTools.exQuery(sql, StudyRoom.class,studyRoomId);
		return list;
	}

	/**
	 * 通过模糊查询分页显示自习室信息
	 */
	@Override
	public PageData<StudyRoom> getStudyRoomByPage(int page, int pageSize, String keywords) {
		// TODO Auto-generated method stub
		PageData<StudyRoom> pd = DBTools.exQueryByPage("select * from studyroom where rstatus like ? or ano like ? or rno like ? or rposition like ?"
				, StudyRoom.class
				,page, pageSize,keywords,keywords,keywords,"%" + keywords + "%");
		return pd;
	}

}
